﻿using EntityPet;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PET.Tools;
using System.Security.Principal;

namespace PET.Controllers
{
    /// <summary>
    /// 用户界面
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly PetDbContext db;
        public UserController(PetDbContext _context)
        {
            db = _context;

        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="_Account">用户输入的账户</param>
        /// <param name="_Password">用户输入的密码</param>
        /// <returns></returns>
        public Result Login(string _Account,string _Password)
        {
            Result result = new Result();
            Pet_User? account = db.Pet_User.Where(s => s.Account == _Account && s.Password == _Password).SingleOrDefault();
            result.Data = account;
            result.Name = "";
            result.Code = account == null ? 404 : 200;
            return result;
        }


        [HttpGet]
        public List<Pet_User> GetBySelect([FromHeader] string Account,[FromHeader] string Email)
        {
            List<Pet_User> user = new List<Pet_User>();

            if (string.IsNullOrEmpty(Account))
            {
                user = db.Pet_User.Where(s => s.Account ==Account).OrderBy(s => s.Account).ToList();
            }
            if (string.IsNullOrEmpty(Email))
            {
                user = db.Pet_User.Where(s => s.Email == Email).OrderBy(s => s.Email).ToList();
            }
            return user;
        }

        /// <summary>
        /// 查询用户信息
        /// </summary>
        /// <returns></returns>
        public Result Get()
        {
            Result result = new Result();

            var resulrData = db.Pet_User.ToList<Pet_User>();
            result.Data = resulrData;
            result.Code = 200;
            return result;
        }

        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>

        [HttpPost]
        public Result AddUser([FromBody] Pet_User user)
        {
            Result result = new Result();
            db.Pet_User.Add(user);
            result.Data = db.SaveChanges();
            return result;
        }

        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result DelUser(Guid id)
        {
            Result result = new Result();
            Pet_User? user = db.Pet_User.Where(w => w.ID == id).SingleOrDefault();
            if (user != null)
            db.Pet_User.Remove(user);
            result.Data = db.SaveChanges();
            return result;
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPut]
        public Result UpUser([FromBody] Pet_User user)
        {
            Result result = new Result();
            db.Pet_User.Update(user);
            result.Data =db.SaveChanges();
            return result;
        }
    }
}
